package com.muyu.system.controller;

import cn.hutool.http.Header;
import cn.hutool.http.HttpGlobalConfig;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.PageUtils;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.system.Vo.FindCartVO;
import com.muyu.system.domain.*;
import com.muyu.system.dto.AddCartDTO;
import com.muyu.system.dto.FindCartDTO;
import com.muyu.system.req.CartItemDto;
import com.muyu.system.req.ShoppingCartDto;
import com.muyu.system.req.ShoppingCartModificationDto;
import com.muyu.system.service.IDanmoShoppingCartService;
import com.muyu.system.service.IDanmoShoppingProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;


/**
 * 购物车Controller
 *
 * @author kunzong
 * @date 2024-09-29
 */
@RestController
@RequestMapping("/cart")
public class DanmoShoppingCartController extends BaseController {
    private final IDanmoShoppingCartService danmoShoppingCartService;

    @Autowired
    public DanmoShoppingCartController(IDanmoShoppingCartService danmoShoppingCartService) {
        this.danmoShoppingCartService = danmoShoppingCartService;
    }

    @Autowired
    private IDanmoShoppingProductsService danmoShoppingProductsService;

    /**
     * 查询购物车列表
     */
//    @RequiresPermissions("system:cart:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoShoppingProducts danmoShoppingProducts) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.warn("请登录");
        }
        PageHelper.startPage(danmoShoppingProducts.getPageNum(), danmoShoppingProducts.getPageSize());
//        List<DanmoShoppingCart> list = danmoShoppingCartService.selectDanmoShoppingCartList(loginUser);
//        List<CartItemDto> list = danmoShoppingCartService.selectDanmoShoppingCartList(loginUser);
        ShoppingCartDto shoppingCartDto = danmoShoppingCartService.selectDanmoShoppingCartList(loginUser);
        if (shoppingCartDto.getDanmoShoppingCartLists().size() == 0){
            return Result.success(1,"小车空啦，加进来看价格更方便！");
        }

        return Result.success(shoppingCartDto);
    }

    /**
     * 分页获取购物车列表
     * @return
     */
    @PostMapping("/find/cart")
    public Result<PageInfo<FindCartVO>> findCart(@RequestBody @Valid FindCartDTO findCartDTO){
        IPage<FindCartVO> cart = danmoShoppingCartService.findCart(findCartDTO);
        return Result.success(PageUtils.convertToPageInfo(cart));
    }

    /**
     * 删除购物车商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteCart(@PathVariable Long id){
        danmoShoppingCartService.deleteCart(id);
        return Result.success("成功！");
    }

    /**
     * 批量删除购物车
     * @param ids
     * @return
     */
    @PostMapping("/delete/cart")
    public Result<String> deleteCartList(@RequestBody List<Long> ids){
        danmoShoppingCartService.deleteCartList(ids);
        return Result.success("成功！");
    }

    /**
     * 导出购物车列表
     */
//    @RequiresPermissions("system:cart:export")
/*    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoShoppingCart danmoShoppingCart) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
//        List<DanmoShoppingCart> list = danmoShoppingCartService.selectDanmoShoppingCartList(danmoShoppingCart);
        List<CartItemDto> list = danmoShoppingCartService.selectDanmoShoppingCartList(loginUser);
        ExcelUtil<CartItemDto> util = new ExcelUtil<CartItemDto>(CartItemDto.class);
        util.exportExcel(response, list, "购物车数据");
    }*/

    /**
     * 获取购物车详细信息
     */
//    @RequiresPermissions("system:cart:query")
    @PostMapping(value = "/details/{productId}/{shoppingSkuId}")
    public Result getInfo(@PathVariable("productId") Long productId, @PathVariable("shoppingSkuId") Long shoppingSkuId) {
        DanmoShoppingProducts danmoShoppingProducts = danmoShoppingProductsService.selectDanmoShoppingProductsAndSkuById(productId,shoppingSkuId);
        return Result.success(danmoShoppingProducts);
    }
    /**
     * 新增购物车
     */
//    @RequiresPermissions("system:cart:add")
/*    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoShoppingCart danmoShoppingCart) {
        return toAjax(danmoShoppingCartService.insertDanmoShoppingCart(danmoShoppingCart));
    }*/

    /**
     * 修改购物车
     */
//    @RequiresPermissions("system:cart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody ShoppingCartModificationDto shoppingCartModificationDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoShoppingCartService.updateDanmoShoppingCart(shoppingCartModificationDto);
    }

    /**
     * 删除购物车
     */
//    @RequiresPermissions("system:cart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoShoppingCartService.deleteDanmoShoppingCartByIds(ids);
    }
    /**
     * 购物车结算
     */
    @Log(title = "购物车", businessType = BusinessType.SETTLEMENT)
    @PostMapping("checkout/{ids}")
    public Result settlement(@PathVariable Long[] ids) {
        return danmoShoppingCartService.settlementDanmoShoppingCartByIds(ids);
    }

    /**
     * 新增购物车收藏
     */
//    @RequiresPermissions("system:collect:add")
    @Log(title = "购物车商品收藏", businessType = BusinessType.INSERT)
    @GetMapping("add/{productsIds}")
    public Result add(@PathVariable Long[] productsIds) {
        return danmoShoppingCartService.insertDanmoMarketCollect(productsIds);
    }

    /**
     * 添加到购物车
     */
    @PostMapping("/addCart")
    public Result addCart(@RequestBody AddCartDTO addCartDTO) {
        return danmoShoppingProductsService.addCart(addCartDTO);
    }
}
